<?php
namespace app\admin\controller;

use think\facade\Db;
use app\common\model\Order as OrderModel;
use app\common\model\OrderGoods as GoodsModel;
use app\common\model\OrderExpress as ExpressModel;
/**
 * 订单控制器
 */
class Order extends Admin 
{
	/**
	 * 订单列表
	 */
	public function index(){
		$order_no = $this->request->param('order_no/s');
		//$status = $this->request->param('status/d',-1);

		$map = [];
		$query = [];
		if(!empty($order_no)){
			$map['order_no'] = $order_no;
			$query['order_no'] = $order_no;			
		}

		$list = OrderModel::with('member')->where($map)->order('id desc')->paginate(15, false, ['query' => $query]);

		foreach ($list as $key => $value) {
			$list[$key]['goods'] = GoodsModel::where('order_id',$value['id'])->order('id desc')->select();
		}

		$this->assign('title','全部订单');
		$this->assign('list', $list);
		return $this->fetch();
	}

	/**
	 * 待付款订单
	 * @return array
	 */
	public function status0(){
		$order_no = $this->request->param('order_no/s');
		$map['pay_status'] = 0;
		$query = [];
		if(!empty($order_no)){
			$map['order_no'] = $order_no;
			$query['order_no'] = $order_no;			
		}

		$list = OrderModel::with('member')->where($map)->order('id desc')->paginate(15, false, ['query' => $query]);

		foreach ($list as $key => $value) {
			$list[$key]['goods'] = GoodsModel::where('order_id',$value['id'])->order('id desc')->select();
		}

		$this->assign('title','待付款订单');
		$this->assign('list', $list);
		return $this->fetch('index');
	}

	/**
	 * 待发货订单
	 */
	public function status1(){
		$order_no = $this->request->param('order_no/s');
		$map['pay_status'] = 1;
		$map['shipping_status'] = 0;
		$query = [];
		if(!empty($order_no)){
			$map['order_no'] = $order_no;
			$query['order_no'] = $order_no;			
		}

		$list = OrderModel::with('member')->where($map)->order('id desc')->paginate(15, false, ['query' => $query]);

		foreach ($list as $key => $value) {
			$list[$key]['goods'] = GoodsModel::where('order_id',$value['id'])->order('id desc')->select();
		}

		$this->assign('title','待发货订单');
		$this->assign('list', $list);
		return $this->fetch('index');
	}

	/**
	 * 已发货订单
	 * @return array
	 */
	public function status2(){
		$order_no = $this->request->param('order_no/s');
		$map['pay_status'] = 1;
		$map['shipping_status'] = 1;
		$query = [];
		if(!empty($order_no)){
			$map['order_no'] = $order_no;
			$query['order_no'] = $order_no;			
		}

		$list = OrderModel::with('member')->where($map)->order('id desc')->paginate(15, false, ['query' => $query]);

		foreach ($list as $key => $value) {
			$list[$key]['goods'] = GoodsModel::where('order_id',$value['id'])->order('id desc')->select();
		}

		$this->assign('title','已发货订单');
		$this->assign('list', $list);
		return $this->fetch('index');
	}

	/**
	 * 已完成订单
	 * @return array
	 */
	public function status3(){
		$order_no = $this->request->param('order_no/s');
		$map['pay_status'] = 1;
		$map['shipping_status'] = 1;
		$map['order_status'] = 1;
		$query = [];
		if(!empty($order_no)){
			$map['order_no'] = $order_no;
			$query['order_no'] = $order_no;			
		}

		$list = OrderModel::with('member')->where($map)->order('id desc')->paginate(15, false, ['query' => $query]);

		foreach ($list as $key => $value) {
			$list[$key]['goods'] = GoodsModel::where('order_id',$value['id'])->order('id desc')->select();
		}

		$this->assign('title','已完成订单');
		$this->assign('list', $list);
		return $this->fetch('index');
	}

	/**
	 * 已取消订单
	 * @return array
	 */
	public function status4(){
		$order_no = $this->request->param('order_no/s');		
		$map['order_status'] = -1;
		$query = [];
		if(!empty($order_no)){
			$map['order_no'] = $order_no;
			$query['order_no'] = $order_no;			
		}

		$list = OrderModel::with('member')->where($map)->order('id desc')->paginate(15, false, ['query' => $query]);

		foreach ($list as $key => $value) {
			$list[$key]['goods'] = GoodsModel::where('order_id',$value['id'])->order('id desc')->select();
		}

		$this->assign('title','已取消订单');
		$this->assign('list', $list);
		return $this->fetch('index');
	}
	

	/**
	 * 查看订单
	 * @param  integer $id 
	 * @return array
	 */
	public function info($id = 0){
		empty($id) && $this->error('参数错误');

		$info = OrderModel::with('member')->where('id',$id)->find();
		$goods = GoodsModel::where('order_id',$id)->select();
		$express = ExpressModel::where('order_id',$id)->select();
		$progress = 3;
		$this->assign('progress', $progress);
		$this->assign('info', $info);
		$this->assign('goods', $goods);
		$this->assign('express', $express);
		return $this->fetch();
	}

	/**
	 * 删除订单
	 */
	public function del($id = 0){
		empty($id) && $this->error('参数错误');

		if(is_array($id)){
			$map[] = ['id','in',$id];
		}elseif(is_numeric($id)){
			$map[] = ['id','=',$id];
		}

		if($query = OrderModel::where($map)->delete()){
			$this->success('删除成功');
		}else{
			$this->error('删除失败');
		}
	}

	/**
	 * 导出excel
	 */
	public function excel(){
		$map = [];
		$list = OrderModel::where($map)->order('id desc')->select();

		echo json_encode($list);		
	}

	/**
	 * 发货
	 */
	public function send($id = 0){
		if($this->request->isPost()){
			$data = $this->request->post();
			//验证
			$result = $this->validate($data,[
				'company_id|物流公司' => 'require',
				'express_no|物流单号' => 'require'
			]);
			// 验证失败 输出错误信息
			if(true !== $result) $this->error($result);

			$update['company_id'] = $data['company_id'];
			$update['shipping_status'] = 1;
			$update['consign_time'] = request()->time();
			if($res = OrderModel::where('id',$data['id'])->update($update)){
				$company_name = Db::name('express')->where('id',$company_id)->value('name');
				$data['order_id'] = $id;
				$data['uid'] = UID;
				$data['company'] = $company_name;
				if($rs = ExpressModel::create($data)){
					$this->success('发货成功');
				}else{
					$this->error('发货失败');
				}
			}else{
				$this->error('发货失败');
			}
		}else{
			$this->assign('id',$id);
			$this->assign('company',$company);

			return $this->fetch();
		}
	}
}